#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include "sock.h"

int main(int argc, char **argv){

 	int listen_fd;//声明 服务器监听fd
 	int conn_fd;//声明 连接的fd，也就是accept之后的
 	struct sockaddr_in client_addr;
 	socklen_t len = sizeof(client_addr);

 	create_bind_listen(&listen_fd, 9999, 5);
	
	char path[512];
 	while(1){
 		printf("----------------ready for file transfer-------------\n"); 
 		conn_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &len);

		if(conn_fd > 0){
			 printf("got connection from ip:%s, port:%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); 
			 
			 printf("input the path:");
			 scanf("%s",path);
			 path[strlen(path)]='\0';
			 printf("%s-\n",path);
			 
			 Send(path, conn_fd);
		}
		else if(conn_fd < 0){
			printf("connection error ...\n");
			continue;
		}
		close(conn_fd);
 	}

 	close(listen_fd);
 	return 0;

}
